import { observable, action } from 'mobx';
import { message } from 'antd';
import { WeaTools } from 'ecCom';
import { WeaTableNew } from 'comsMobx';
import Api from '../../apis/view';

const { TableStore } = WeaTableNew;
const { Base64 } = WeaTools;
const initalState = {
  staticAllSpan: {},
  staticViewBean: {},
  staticMailContent: '',
  staticViewRight: 0,
  staticIsSimple: 0,
  readListCount: [],
  staticReadListVisible: false,
  staticReadSeleted: '',
};
export default class StaticViewStore {
  @observable readListTableStore = new TableStore();
  @observable staticLoading = true;
  @observable staticViewRight = initalState.staticViewRight;
  @observable staticAllSpan = initalState.staticAllSpan;
  @observable staticViewBean = initalState.staticViewBean;
  @observable staticMailContent = initalState.staticMailContent;
  @observable staticIsSimple = initalState.staticIsSimple;
  @observable readListCount = initalState.readListCount;
  @observable staticReadListVisible = initalState.staticReadListVisible;
  @observable staticReadSeleted = initalState.staticReadSeleted;

  @action setStoreState = (params) => {
    Object.keys(params).forEach((key) => {
      this[key] = params[key];
    });
  };

  @action clear = () => this.setStoreState(initalState);

  @action getViewRight = (params) => {
    this.staticLoading = true;
    Api.getRight(params).then((result) => {
      this.staticViewRight = result.viewRight;
      if (result.viewRight * 1 === 1) {
        Promise.all([
          Api.getSentTo(params).then((res) => {
            // 收件人&&发件人信息
            if (res.viewRight * 1 === 1 && res.status * 1 === 1) {
              this.staticAllSpan = res.allSpan;
            }
          }),
          Api.getBase(params).then((res) => {
            // 获取邮件基本信息
            if (res.viewRight * 1 === 1 && res.status * 1 === 1) {
              this.staticViewBean = res.viewBean;
              this.staticIsSimple = res.viewBean.showmode;
            }
          }),
          Api.getContent(params).then((res) => {
            // 获取邮件内容信息
            if (res.viewRight * 1 === 1 && res.status * 1 === 1) {
              this.staticMailContent = Base64.decode(res.mailContent);
            }
          }),
        ]).then(() => {
          this.staticLoading = false;
        });
      } else {
        this.staticLoading = false;
      }
    });
  };

  @action getReadCount = (params) => {
    Api.getReadCount(params).then((res) => {
      try {
        if (res.status === '1') {
          this.readListCount = res.topList;
        } else {
          message.error(res.msg);
        }
      } catch (e) {
        message.error(e);
      }
    });
  };

  @action getReadList = (params) => {
    Api.getReadList(params).then((res) => {
      try {
        if (res.status === '1') {
          this.readListTableStore.getDatas(res.sessionkey, params.current || 1);
        } else {
          message.error(res.msg);
        }
      } catch (e) {
        message.error(e);
      }
    });
  };
}
